﻿Imports System.ComponentModel
Imports LSW.Windows.Controls.Forms
Imports LSW.Image
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text

Namespace QQ
    ''' <summary>
    ''' Button
    ''' </summary>
    <DefaultEvent("Click")> _
    <DefaultProperty("Text")> _
    <ToolboxBitmap(GetType(Button))> _
    Public Class QQButton
        Inherits Button
#Region "变量"
        ''' <summary>
        ''' 鼠标状态
        ''' </summary>
        Private _mouseState As EMouseState = EMouseState.Normal
        ''' <summary>
        ''' 文本对齐方式
        ''' </summary>
        Private _textAlign As TextFormatFlags = TextFormatFlags.VerticalCenter Or TextFormatFlags.HorizontalCenter
        ''' <summary>
        ''' 默认时的按钮图片
        ''' </summary>
        Private _normalImage As Drawing.Image = Nothing
        ''' <summary>
        ''' 鼠标按下时的图片
        ''' </summary>
        Private _downImage As Drawing.Image = Nothing
        ''' <summary>
        ''' 鼠标划过时的图片
        ''' </summary>
        Private _moveImage As Drawing.Image = Nothing
        ''' <summary>
        ''' 是否按下了鼠标
        ''' </summary>
        Private _isShowBorder As Boolean = True

#End Region

#Region "构造函数"
        ''' <summary>
        ''' 实例化 Paway.Windows.Forms.QQButton 新的实例。
        ''' </summary>
        Public Sub New()
            Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.Selectable Or ControlStyles.DoubleBuffer Or ControlStyles.SupportsTransparentBackColor Or ControlStyles.UserPaint, True)
            Me.SetStyle(ControlStyles.Opaque, False)
            MyBase.BackColor = Color.Transparent
            Me.UpdateStyles()
        End Sub

#End Region

#Region "属性"
        ''' <summary>
        ''' 默认大小
        ''' </summary>
        Protected Overrides ReadOnly Property DefaultSize() As Size
            Get
                Return New Size(75, 28)
            End Get
        End Property
        ''' <summary>
        ''' 默认图片
        ''' </summary>
        <Description("默认图片")> _
        Public Overridable Property NormalImage() As Drawing.Image
            Get
                If Me._normalImage Is Nothing Then
                    Me._normalImage = AssemblyHelper.GetImage("bt_normal.png")
                End If
                Return Me._normalImage
            End Get
            Set(value As Drawing.Image)
                Me._normalImage = value
                MyBase.Invalidate()
            End Set
        End Property
        ''' <summary>
        ''' 鼠标按下时的图片
        ''' </summary>
        <Description("鼠标按下时的图片")> _
        Public Overridable Property DownImage() As Drawing.Image
            Get
                If Me._downImage Is Nothing Then
                    Me._downImage = AssemblyHelper.GetImage("down.png")
                End If
                Return Me._downImage
            End Get
            Set(value As Drawing.Image)
                Me._downImage = value
                MyBase.Invalidate()
            End Set
        End Property
        ''' <summary>
        ''' 鼠标划过时的图片
        ''' </summary>
        <Description("鼠标划过时的图片")> _
        Public Overridable Property MoveImage() As Drawing.Image
            Get
                If Me._moveImage Is Nothing Then
                    Me._moveImage = AssemblyHelper.GetImage("highlight.png")
                End If
                Return Me._moveImage
            End Get
            Set(value As Drawing.Image)
                Me._moveImage = value

                MyBase.Invalidate()
            End Set
        End Property
        ''' <summary>
        ''' 是否显示发光边框
        ''' </summary>
        <Description("是否显示发光边框")> _
        Public Overridable Property IsShowBorder() As Boolean
            Get
                Return Me._isShowBorder
            End Get
            Set(value As Boolean)
                Me._isShowBorder = value
            End Set
        End Property
        ''' <summary>
        ''' 与控件相关的文本
        ''' </summary>
        <DefaultValue("QQButton")> _
        Public Overrides Property Text() As String
            Get
                Return MyBase.Text
            End Get
            Set(value As String)
                MyBase.Text = value
                MyBase.Invalidate(Me.TextRect)
            End Set
        End Property
        ''' <summary>
        ''' 按钮上显示的图片
        ''' </summary>
        <Description("按钮上显示的图片")> _
        Public Overridable Shadows Property Image() As Drawing.Image
            Get
                Return MyBase.Image
            End Get
            Set(value As Drawing.Image)
                MyBase.Image = value
                MyBase.Invalidate()
            End Set
        End Property

        ''' <summary>
        ''' 按钮上文字的对齐方式
        ''' </summary>
        <Description("按钮上文字的对齐方式")> _
        Public Shadows Property TextAlign() As ContentAlignment
            Get
                Return MyBase.TextAlign
            End Get
            Set(value As ContentAlignment)
                MyBase.TextAlign = value
                Select Case MyBase.TextAlign
                    Case ContentAlignment.BottomCenter
                        Me._textAlign = TextFormatFlags.Bottom Or TextFormatFlags.HorizontalCenter Or TextFormatFlags.SingleLine
                        Exit Select
                    Case ContentAlignment.BottomLeft
                        Me._textAlign = TextFormatFlags.Bottom Or TextFormatFlags.Left Or TextFormatFlags.SingleLine
                        Exit Select
                    Case ContentAlignment.BottomRight
                        Me._textAlign = TextFormatFlags.Bottom Or TextFormatFlags.Right Or TextFormatFlags.SingleLine
                        Exit Select
                    Case ContentAlignment.MiddleCenter
                        Me._textAlign = TextFormatFlags.SingleLine Or TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter
                        Exit Select
                    Case ContentAlignment.MiddleLeft
                        Me._textAlign = TextFormatFlags.Left Or TextFormatFlags.VerticalCenter Or TextFormatFlags.SingleLine
                        Exit Select
                    Case ContentAlignment.MiddleRight
                        Me._textAlign = TextFormatFlags.Right Or TextFormatFlags.VerticalCenter Or TextFormatFlags.SingleLine
                        Exit Select
                    Case ContentAlignment.TopCenter
                        Me._textAlign = TextFormatFlags.Top Or TextFormatFlags.HorizontalCenter Or TextFormatFlags.SingleLine
                        Exit Select
                    Case ContentAlignment.TopLeft
                        Me._textAlign = TextFormatFlags.Top Or TextFormatFlags.Left Or TextFormatFlags.SingleLine
                        Exit Select
                    Case ContentAlignment.TopRight
                        Me._textAlign = TextFormatFlags.Top Or TextFormatFlags.Right Or TextFormatFlags.SingleLine
                        Exit Select
                End Select
                MyBase.Invalidate(Me.TextRect)
            End Set
        End Property

        ''' <summary>
        ''' 整个按钮的区域
        ''' </summary>
        Friend ReadOnly Property AllRect() As Rectangle
            Get
                Return New Rectangle(0, 0, Me.Width, Me.Height)
            End Get
        End Property
        ''' <summary>
        ''' 文字区域
        ''' </summary>
        Friend ReadOnly Property TextRect() As Rectangle
            Get
                Return New Rectangle(2, 2, Me.AllRect.Width - 4, Me.AllRect.Height - 4)
            End Get
        End Property
        ''' <summary>
        ''' 鼠标状态
        ''' </summary>
        Friend Property MouseState() As EMouseState
            Get
                Return Me._mouseState
            End Get
            Set(value As EMouseState)
                Me._mouseState = value
                MyBase.Invalidate()
            End Set
        End Property

#End Region

#Region "Override 方法"
        ''' <summary>
        ''' 引发 System.Windows.Forms.Form.Paint 事件。
        ''' </summary>
        ''' <param name="pevent">包含事件数据的 System.Windows.Forms.PaintEventArgs。</param>
        Protected Overrides Sub OnPaint(pevent As PaintEventArgs)
            Dim g As Graphics = pevent.Graphics
            g.SmoothingMode = SmoothingMode.AntiAlias
            g.TextRenderingHint = TextRenderingHint.AntiAlias
            'g.DrawImage(AssemblyHelper.GetAssemblyImage("Button.all_btn_White-side.png"), this.AllRect);
            If MyBase.Enabled Then
                If Me.IsShowBorder Then
                    If Me.Focused Then
                        '得到焦点的时候，绘制边框
                        Using light As Drawing.Image = AssemblyHelper.GetImage("Light.png")
                            'g.DrawImage(light, this.AllRect);
                            DrawHelper.RendererBackground(g, Me.AllRect, light, True)
                        End Using
                    End If
                End If

                Select Case Me.MouseState
                    Case EMouseState.Leave, EMouseState.Normal
                        If MyBase.Focused Then
                            If Me.IsShowBorder Then
                                Using focus As Drawing.Image = AssemblyHelper.GetImage("focus.png")
                                    DrawHelper.RendererBackground(g, Me.TextRect, focus, True)
                                End Using
                            Else
                                DrawHelper.RendererBackground(g, Me.TextRect, Me.NormalImage, True)
                            End If
                        Else
                            DrawHelper.RendererBackground(g, Me.TextRect, Me.NormalImage, True)
                        End If
                        Exit Select
                    Case EMouseState.Up, EMouseState.Move
                        DrawHelper.RendererBackground(g, Me.TextRect, Me.MoveImage, True)
                        Exit Select
                    Case EMouseState.Down
                        DrawHelper.RendererBackground(g, Me.TextRect, Me.DownImage, True)
                        Exit Select
                End Select

                'if (this.Image != null)
                '{
                'int y = (this.Height - this.Image.Height) / 2;
                'int x = 10;

                'if (!string.IsNullOrEmpty(this.Text))
                '    x = (this.AllRect.Width - (TextHelper.GetStringLen(this.Text) * 6 + this.Image.Width)) / 2;
                'else
                '    x = (this.Width - this.Image.Width) / 2;//将图片绘制在按钮居中位置
                'Rectangle rect = new Rectangle(x, y, this.Image.Width, this.Image.Height);
                'Rectangle imgRect = new Rectangle(0, 0, this.Image.Width, this.Image.Height);
                '绘制图片
                'g.DrawImage(this.Image, rect, imgRect, GraphicsUnit.Pixel);
                '绘制文字
                'Rectangle textRect = new Rectangle(this.TextRect.X + this.Image.Width, this.TextRect.Y, this.TextRect.Width, this.TextRect.Height);
                'TextRenderer.DrawText(g, this.Text, this.Font, textRect, this.ForeColor, _textAlign);
                '}
                'else
                '{
                '绘制按钮上的文字
                '}
                TextRenderer.DrawText(g, Me.Text, Me.Font, Me.TextRect, Me.ForeColor, Me._textAlign)
            Else
                Using gray As Drawing.Image = AssemblyHelper.GetImage("gray.png")
                    DrawHelper.RendererBackground(g, Me.TextRect, gray, True)
                End Using
                TextRenderer.DrawText(g, Me.Text, Me.Font, Me.TextRect, Color.Gray, Me._textAlign)
            End If
        End Sub
        ''' <summary>
        ''' 引发 System.Windows.Forms.Form.MouseEnter 事件。
        ''' </summary>
        ''' <param name="e">包含事件数据的 System.EventArgs。</param>
        Protected Overrides Sub OnMouseEnter(e As EventArgs)
            MyBase.OnMouseEnter(e)
            Me.MouseState = EMouseState.Move
        End Sub
        ''' <summary>
        ''' 引发 System.Windows.Forms.Form.MouseLeave 事件。
        ''' </summary>
        ''' <param name="e">包含事件数据的 System.EventArgs。</param>
        Protected Overrides Sub OnMouseLeave(e As EventArgs)
            MyBase.OnMouseLeave(e)
            Me.MouseState = EMouseState.Leave
        End Sub
        ''' <summary>
        ''' 引发 System.Windows.Forms.Form.MouseDown 事件。
        ''' </summary>
        ''' <param name="mevent">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
        Protected Overrides Sub OnMouseDown(mevent As MouseEventArgs)
            MyBase.OnMouseDown(mevent)
            Me.MouseState = EMouseState.Down
        End Sub
        ''' <summary>
        ''' 引发 System.Windows.Forms.Form.MouseUp 事件。
        ''' </summary>
        ''' <param name="mevent">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
        Protected Overrides Sub OnMouseUp(mevent As MouseEventArgs)
            MyBase.OnMouseUp(mevent)
            Me.MouseState = EMouseState.Up
        End Sub
#End Region
    End Class

End Namespace